package managesystem.login;

import managesystem.*;
import managesystem.help.HelpUI;
import managesystem.manager.ManagerUI;
import managesystem.poverty.PovertyUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.util.ArrayList;

/**
 * @author The setting sun
 * @version 1.0
 * @title: Login
 * @description: 登录界面Final
 * @date 2021/12/2 14:45
 */
public class Login {
    JFrame frame = new JFrame("扶贫管理系统");

    JPanel titleP = new JPanel();
    JLabel title = new JLabel("扶贫管理系统");

    JPanel p1 = new JPanel();
    JLabel user = new JLabel("账号：");
    JTextField userText = new JTextField(20);

    JPanel p2 = new JPanel();
    JLabel pwd = new JLabel("密码：");
    JTextField pwdText = new JTextField(20);

    JPanel p3 = new JPanel();
    JLabel level = new JLabel("权限：");
    JComboBox levelBox = new JComboBox();

    JPanel hintP = new JPanel();
    JLabel hint = new JLabel();

    JPanel buttonP = new JPanel();
    JButton loginButton = new JButton("登录");
    JButton registerButton = new JButton("注册");
    JButton quitButton = new JButton("退出");

    JLabel hide1 = new JLabel();
    JLabel hide3 = new JLabel();
    JLabel hide4 = new JLabel();
    JLabel hide5 = new JLabel();
    JLabel hide6 = new JLabel();
    JLabel hide7 = new JLabel();
    JLabel hide8 = new JLabel();

    public static void main(String[] args) {
        new Login().init();
    }

    public void init() {
        title.setFont(new Font("楷体", Font.BOLD, 36));
        user.setFont(new Font("微软雅黑", Font.BOLD, 18));
        pwd.setFont(new Font("微软雅黑", Font.BOLD, 18));
        level.setFont(new Font("微软雅黑", Font.BOLD, 18));

        hint.setFont(new Font("微软雅黑", Font.BOLD, 16));
        hint.setForeground(Color.RED);

        loginButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
        registerButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
        quitButton.setFont(new Font("微软雅黑", Font.BOLD, 16));


        levelBox.addItem("请选择你的身份");
        levelBox.addItem("管理员");
        levelBox.addItem("扶贫人员");
        levelBox.addItem("贫困人员");

        titleP.add(title);

        p1.add(user);
        p1.add(userText);

        p2.add(pwd);
        p2.add(pwdText);

        p3.add(level);
        p3.add(levelBox);

        hintP.add(hint);

        buttonP.add(loginButton);
        buttonP.add(hide3);
        buttonP.add(hide5);
        buttonP.add(hide7);
        buttonP.add(registerButton);
        buttonP.add(hide4);
        buttonP.add(hide6);
        buttonP.add(hide8);
        buttonP.add(quitButton);

        frame.setLayout(new GridLayout(7, 1));
        frame.add(titleP);
        frame.add(hide1);
        frame.add(p1);
        frame.add(p2);
        frame.add(p3);
        frame.add(hintP);
        frame.add(buttonP);

        frame.setVisible(true);
        frame.setSize(500, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        userText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                userText.setText("");
            }
        });

        pwdText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                pwdText.setText("");
            }
        });

        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection connection = JDBCUtils.getConnection();

                String sql = "select * from user_account";
                ArrayList<UserAccount> accounts = JDBCUtils.verificationUserAccount(connection, sql);

                int index = levelBox.getSelectedIndex();
                for (int i = 0; i < accounts.size(); i++) {
                    //贫困人员
                    if (userText.getText().equals(accounts.get(i).getAccount())
                            && pwdText.getText().equals(accounts.get(i).getPassword())
                    ) {

                        if (index == accounts.get(i).getLevel()) {
                            frame.dispose();
                            new PovertyUI().init();
                        } else {
                            hint.setText("你选择的身份有误，请重新选择！");
                            return;
                        }
                    } else {
                        hint.setText("账号或密码错误，请重新输入！");
                    }

                    //帮扶人员
                    if (userText.getText().equals(accounts.get(i).getAccount())
                            && pwdText.getText().equals(accounts.get(i).getPassword())) {
                        if (index == accounts.get(i).getLevel()) {
                            frame.dispose();
                            new HelpUI().init();
                        } else {
                            hint.setText("你选择的身份有误，请重新选择！");
                            return;
                        }
                    } else {
                        hint.setText("账号或密码错误，请重新输入！");
                    }

                    //管理员
                    if (userText.getText().equals(accounts.get(i).getAccount())
                            && pwdText.getText().equals(accounts.get(i).getPassword())
                    ) {
                        if (index == accounts.get(i).getLevel()) {
                            frame.dispose();
                            new ManagerUI().init();
                        } else {
                            hint.setText("你选择的身份有误，请重新选择！");
                            return;
                        }
                    } else {
                        hint.setText("账号或密码错误，请重新输入！");
                    }
                }

                JDBCUtils.closeResources(connection, null);
            }
        });

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                new Register().init();
            }
        });

        quitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(1);
            }
        });
    }
}
